package com.Shuang.TCP2;

import java.io.DataInputStream;
import java.io.InputStream;
import java.net.Socket;

public class ClientReadThread extends Thread{

    private Socket socket;

    public ClientReadThread(Socket socket) {
        this.socket = socket;
    }
    @Override
    public void run() {
        try {

            //3 连接上了之后调用字节输入流将数据读进来
            InputStream is = socket.getInputStream();

            //4 将低级的自己输入流包装成数据输入流，保持跟客户端一致
            DataInputStream dis = new DataInputStream(is);

            while (true) {
                try {
                    String rs = dis.readUTF();  //有客户端下线，会在这有异常在下面捕获
                    System.out.println(rs);
                } catch (Exception e) {
                    System.out.println(socket.getRemoteSocketAddress() + ": 我下线啦~");
                    dis.close();
                    socket.close();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
